package Second.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import Second.Utils.ContentDatas;

public class SecondMainActivity extends AppCompatActivity {

    private ListView secondListview;
    List<String>list;
    SecondItemAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_main);
        initView();
        list=new ArrayList<>();
        list=ContentDatas.dailyKindList;
        adapter=new SecondItemAdapter(this,list);
        secondListview.setAdapter(adapter);

    }

    private void initView() {
        secondListview = (ListView) findViewById(R.id.second_listview);
    }
    class SecondItemAdapter extends BaseAdapter {

        private List<String> objects = new ArrayList<>();

        private Context context;
        private LayoutInflater layoutInflater;

        public SecondItemAdapter(Context context,List<String> objects) {
            this.objects=objects;
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public Object getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.second_item, null);
                holder=new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder=(ViewHolder)convertView.getTag();
            }
            holder.secondTv.setText(objects.get(position));
            holder.layoutSecond.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(new Intent(context,PaoPaoActivity.class));
                }
            });
            return convertView;
        }



        protected class ViewHolder {
            private RelativeLayout layoutSecond;
            private TextView secondTv;
            private ImageView secondIv;

            public ViewHolder(View view) {
                layoutSecond = (RelativeLayout) view.findViewById(R.id.layout_second);
                secondTv = (TextView) view.findViewById(R.id.second_tv);
                secondIv = (ImageView) view.findViewById(R.id.second_iv);
            }
        }
    }

}
